package com.edu.app.config;


import com.edu.app.model.wechat.WechatConfig;
import com.edu.app.service.impl.wechat.WeChatServiceExecutorImpl;
import com.edu.app.service.impl.wechat.WechatBaseServiceImpl;
import com.edu.app.service.impl.wechat.msgroute.WxMpMessageRouter;
import com.edu.app.service.impl.wechat.msgroute.consts.WxConsts;
import com.edu.app.service.impl.wechat.msgroute.impl.MessageHandler.QrScanHandler;
import com.edu.app.service.impl.wechat.msgroute.impl.MessageHandler.QrSubscribeHandler;
import com.edu.app.service.impl.wechat.msgroute.impl.MessageHandler.QrUnsubscribeHandler;
import com.edu.app.service.sys.wechat.WeChatServiceExecutor;
import com.edu.app.service.sys.wechat.WechatBaseService;
import com.edu.app.util.converters.Int2DateConverter;
import com.edu.app.util.converters.Long2DateConverter;
import com.edu.app.util.http.HttpImplConfig;
import org.springframework.beans.BeanWrapperImpl;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.convert.support.DefaultConversionService;


@Configuration
public class WechatConfiguration {

    @Bean
    public WeChatServiceExecutor WechatServiceExecutorBean(WechatConfig wechatConfig, HttpImplConfig httpImplConfig) {
        WeChatServiceExecutor executor =  new WeChatServiceExecutorImpl(httpImplConfig,wechatConfig);
        return executor;
    }

    @Bean
    public WechatBaseService WechatBaseServiceBean(HttpImplConfig httpImplConfig) {
        WechatBaseService wechatBaseService =  new WechatBaseServiceImpl(httpImplConfig);
        return wechatBaseService;
    }

    @Bean
    public WxMpMessageRouter WxMpMessageRouterBean( ) {
        WxMpMessageRouter msgrouter =  new WxMpMessageRouter(null);
//        msgrouter.rule().async(false).msgType(WxConsts.XmlMsgType.EVENT).matcher(new WxMpMessageMatcher(){
//            public boolean match(Map<String,String> wxMessage){
//                boolean bRet = false;
//                String event = wxMessage.get(WxConsts.MsgField.Event);
//                bRet = event.equalsIgnoreCase(WxConsts.EventType.SUBSCRIBE)
//                        || event.equalsIgnoreCase(WxConsts.EventType.SCAN)
//                || event.equalsIgnoreCase(WxConsts.EventType.UNSUBSCRIBE);
//                return bRet;
//            }
//        }).handler(new QrScanHandler()).end();

        msgrouter.rule().async(false).msgType(WxConsts.XmlMsgType.EVENT).event(WxConsts.EventType.SUBSCRIBE).handler(new QrSubscribeHandler()).end();
        msgrouter.rule().async(false).msgType(WxConsts.XmlMsgType.EVENT).event(WxConsts.EventType.UNSUBSCRIBE).handler(new QrUnsubscribeHandler()).end();
        msgrouter.rule().async(false).msgType(WxConsts.XmlMsgType.EVENT).event(WxConsts.EventType.SCAN).handler(new QrScanHandler()).end();

        return msgrouter;
    }


    @Bean
    public BeanWrapperImpl  BeanWrapperBean( ) {
        DefaultConversionService dcserivce = new DefaultConversionService();
        BeanWrapperImpl beanWrapper  = new BeanWrapperImpl();
        dcserivce.addConverter(new Int2DateConverter());
        dcserivce.addConverter(new Long2DateConverter());
        beanWrapper.setConversionService(dcserivce);
        return  beanWrapper;
    }
}
